import request from '@/utils/request'
import type {
  RoleSearchParams,
  RoleFormData,
  RoleResponseData,
  SingleRoleResponseData,
  PermissionResponseData,
  CommonResponseData, RolePermissionsData
} from './types'

enum Api {
  ROLE_LIST = '/system/role/list',
  PERMISSION_TREE = '/system/permission/tree',
  ASSIGN_PERMISSIONS = '/system/role/assignPermissions'
}


// 获取角色列表
export const getRoleListAPI = (params: RoleSearchParams) => request.post<any,RoleResponseData>(Api.ROLE_LIST, params);


// 新增角色
export const addRoleAPI = (data: RoleFormData): Promise<CommonResponseData> => {
  return request({
    url: '/system/role/add',
    method: 'post',
    data
  })
}

// 更新角色
export const updateRoleAPI = (data: RoleFormData): Promise<CommonResponseData> => {
  return request({
    url: `/system/role/update/${data.id}`,
    method: 'put',
    data
  })
}

// 删除角色
export const deleteRoleAPI = (id: number): Promise<CommonResponseData> => {
  return request({
    url: `/system/role/delete/${id}`,
    method: 'delete'
  })
}

// 批量删除角色
export const batchDeleteRoleAPI = (data: { ids: number[] }): Promise<CommonResponseData> => {
  return request({
    url: '/system/role/batchDelete',
    method: 'post',
    data
  })
}

// 获取角色详情
export const getRoleDetailAPI = (id: number): Promise<SingleRoleResponseData> => {
  return request({
    url: `/system/role/detail/${id}`,
    method: 'get'
  })
}

// 获取权限树
export const getPermissionTreeAPI = () => request.get<any,PermissionResponseData>(Api.PERMISSION_TREE);

// 分配角色权限
export const assignRolePermissionsAPI = (data:RolePermissionsData) => request.post<any,CommonResponseData>(Api.ASSIGN_PERMISSIONS, data);

// 获取角色权限
export const getRolePermissionsAPI = (roleId: number): Promise<{ code: number; message: string; data: number[] }> => {
  return request({
    url: `/system/role/permissions/${roleId}`,
    method: 'get'
  })
}
